﻿using System.Collections.Generic;
using UnityEngine;

namespace Player
{
    public class PlayerSaveComp : GuidSaveCompAbs
    {
        public PlayerLevelComp levelComp;
        public PlayerAttributeComp attributeComp;

        private void Awake()
        {
            levelComp = GetComponent<PlayerLevelComp>();
            attributeComp = GetComponent<PlayerAttributeComp>();
        }

        public override Dictionary<string, object> SaveData()
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            // 位置
            data.Add("Position", JsonUtility.ToJson(transform.position));
            //  经验等级保存
            data.Add("Level", levelComp.level);
            data.Add("Experience", levelComp.experience);
            //  属性保存。。。。。
            data.Add("Attribute", JsonUtility.ToJson(attributeComp.attributes));
            return data;
        }

        public override void LoadData(Dictionary<string, object> data)
        {
            if (data != null)
            {
                if (data.ContainsKey("Level"))
                {
                    levelComp.level = (int)data["Level"];
                    levelComp.experience = (float)data["Experience"];
                    attributeComp.attributes =
                        JsonUtility.FromJson<SerializableDic<AttributeType, AttributeValue>>(data["Attribute"]
                            .ToString());
                    attributeComp.level = levelComp.level;
                }

                if (data.ContainsKey("Position"))
                {
                    Vector3 targetPosition = JsonUtility.FromJson<Vector3>(data["Position"].ToString()); // 从存档加载的目标位置
                    transform.position = targetPosition;
                }
            }
        }
    }
}